package com.sgx.learning.luban.thread;

/**
 * 传化公路港物流有限公司 Copyright © 2020版权所有. Transfar All Rights Reserved.
 *
 * @ClassName: single
 * @Prject: luban
 * @Package: com.sgx.learning.luban.thread
 * @Description: TODO
 * @author: 021495
 * @date: 2020/12/11 13:20
 *
 * -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -Xcomp
 */
public class Single {

    private volatile static boolean flag = true;
    private static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            System.out.println("t1 开始");
            while(flag) {
                count++;
//                sleep(800L);
//                sleep(400L);
//                try {
//                    Thread.sleep(0);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println(1);
            }
            System.out.println("t1 结束 : " + count);
        });
        Thread t2 = new Thread(()->{
            System.out.println("t2 变更flag");
            flag = false;
        });
        t1.start();
        Thread.sleep(100);
        t2.start();
        t1.join();
        t2.join();
    }

    private static void sleep(Long sleep) {
        long now = System.nanoTime();
        long end = now;
        while(end - now < sleep) {
            end = System.nanoTime();
        }
    }
}
